<template>
  <div class="contioner">
    <div ref="myChart" :style="{ width: '100%', height: '100%' }"></div>
  </div>
</template>
      
  <script lang="ts" setup>
import * as echarts from 'echarts'
import { Message } from '@arco-design/web-vue'
import { ref, reactive, onMounted, watch } from 'vue'
//引入封装post
import { get } from '@/api/http'
import dayjs from 'dayjs'
//getlist初始化接口参数
let query: any = reactive({
  beginDate: dayjs().format('YYYY-MM-DD'), //当月日期
  enginDate: formatDateTime(
    new Date(Number(new Date().getTime()) + 60 * 1000 * 60 * 24 * 7),
    'yyyy-MM-dd'
  ), //当月日期
    planOrderCode: localStorage.getItem('firmId') || '',
})
function formatDateTime(date: any, format: any) {
  const o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    'H+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? '上午' : '下午', // 上午/下午
    A: date.getHours() < 12 ? 'AM' : 'PM', // AM/PM
  }
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      )
    }
  }
  return format
}
let state: any = null
window.addEventListener('resize', updateChartSize)
function updateChartSize() {
  state.resize()
}
const option = ref({
  title: {
    text: '织厂WIP',
  },
  tooltip: {
    trigger: 'axis',
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true,
  },
  xAxis: {
    type: 'category',
    data: [
      '重量\n计划WIP',
      '重量\n生产WIP',
      '重量\n查布WIP',
      '重量\n修补WIP',
      '重量\n待入仓WIP',
      '重量\n库存WIP',
      '重量\n总WIP',
    ],
  },
  yAxis: {
    type: 'value',
    splitNumber: 2,
    // axisLabel: {
    //     formatter: '{value} %'
    // }
  },
  series: [
    {
      name: '本集团',
      type: 'bar',
      // stack: 'Total',
      data: [120, 132, 101, 134, 90, 230, 120],
      label: { show: true, position: 'top', distance: 10 },
    },
    {
      name: '外加工',
      type: 'bar',
      // stack: 'Total',
      data: [220, 182, 191, 234, 290, 330, 310],
      label: { show: true, position: 'top', distance: 10 },
    },
    {
      name: '汇总',
      type: 'line',
      // stack: 'Total',
      data: [150, 232, 201, 154, 190, 330, 410],
      label: { show: true, position: 'top', distance: 10 },
      lineStyle: {
        color: 'red',
      },
    },
  ],
})
const tableDataList: any = ref([])

//初始化
onMounted(async () => {
  selectJt()
    // state = states()
  // state.setOption(option.value)
})
const myChart: any = ref()
const states = () => {
  return echarts.init(myChart.value)
}
function selectJt() {
  get({
    url: 'planSingle/report/getWipRecordByRealTime',
  }).then((res: any) => {
    if (res.code == 200) {
      tableDataList.value[0] = {
        recordDate: '本集团',
        factoryPlanWeight: res.data.groupPlanWeight,
        factoryPlanNumber: res.data.groupPlanNumber,
        factoryProductionWeight: res.data.groupProductionWeight,
        factoryProductionNumber: res.data.groupProductionNumber,
        factoryCheckWeight: res.data.groupCheckWeight,
        factoryCheckNumber: res.data.groupCheckNumber,
        factoryRepairWeight: res.data.groupRepairWeight,
        factoryRepairNumber: res.data.groupRepairNumber,
        factoryWarehouseWeight: res.data.groupWarehouseWeight,
        factoryWarehouseNumber: res.data.groupWarehouseNumber,
        factoryInventoryWeight: res.data.groupInventoryWeight,
        factoryInventoryNumber: res.data.groupInventoryNumber,
        factoryAllWeight: res.data.groupAllWeight,
        factoryAllNumber: res.data.groupAllNumber,
      }
      tableDataList.value[1] = {
        recordDate: '外加工',
        factoryPlanWeight: res.data.externaPlanWeight,
        factoryPlanNumber: res.data.externaPlanNumber,
        factoryProductionWeight: res.data.externaProductionWeight,
        factoryProductionNumber: res.data.externaProductionNumber,
        factoryCheckWeight: res.data.externaCheckWeight,
        factoryCheckNumber: res.data.externaCheckNumber,
        factoryRepairWeight: res.data.externaRepairWeight,
        factoryRepairNumber: res.data.externaRepairNumber,
        factoryWarehouseWeight: res.data.externaWarehouseWeight,
        factoryWarehouseNumber: res.data.externaWarehouseNumber,
        factoryInventoryWeight: res.data.externaInventoryWeight,
        factoryInventoryNumber: res.data.externaInventoryNumber,
        factoryAllWeight: res.data.externaAllWeight,
        factoryAllNumber: res.data.externaAllNumber,
      }
      tableDataList.value[2] = {
        recordDate: '汇总',
        factoryPlanWeight: res.data.factoryPlanWeight,
        factoryPlanNumber: res.data.factoryPlanNumber,
        factoryProductionWeight: res.data.factoryProductionWeight,
        factoryProductionNumber: res.data.factoryProductionNumber,
        factoryCheckWeight: res.data.factoryCheckWeight,
        factoryCheckNumber: res.data.factoryCheckNumber,
        factoryRepairWeight: res.data.factoryRepairWeight,
        factoryRepairNumber: res.data.factoryRepairNumber,
        factoryWarehouseWeight: res.data.factoryWarehouseWeight,
        factoryWarehouseNumber: res.data.factoryWarehouseNumber,
        factoryInventoryWeight: res.data.factoryInventoryWeight,
        factoryInventoryNumber: res.data.factoryInventoryNumber,
        factoryAllWeight: res.data.factoryAllWeight,
        factoryAllNumber: res.data.factoryAllNumber,
      }
      state = states()
      option.value.series[0].data = [
        tableDataList.value[0].factoryPlanWeight,
        tableDataList.value[0].factoryProductionWeight,
        tableDataList.value[0].factoryCheckWeight,
        tableDataList.value[0].factoryRepairWeight,
        tableDataList.value[0].factoryWarehouseWeight,
        tableDataList.value[0].factoryInventoryWeight,
        tableDataList.value[0].factoryAllWeight,
      ]
      option.value.series[1].data = [
        tableDataList.value[1].factoryPlanWeight,
        tableDataList.value[1].factoryProductionWeight,
        tableDataList.value[1].factoryCheckWeight,
        tableDataList.value[1].factoryRepairWeight,
        tableDataList.value[1].factoryWarehouseWeight,
        tableDataList.value[1].factoryInventoryWeight,
        tableDataList.value[1].factoryAllWeight,
      ]
      option.value.series[2].data = [
        tableDataList.value[2].factoryPlanWeight,
        tableDataList.value[2].factoryProductionWeight,
        tableDataList.value[2].factoryCheckWeight,
        tableDataList.value[2].factoryRepairWeight,
        tableDataList.value[2].factoryWarehouseWeight,
        tableDataList.value[2].factoryInventoryWeight,
        tableDataList.value[2].factoryAllWeight,
      ]
      state.setOption(option.value)
    } else {
      Message.error(res.message)
    }
  })
}
</script>
  <script lang="ts">
export default defineComponent({
  name: 'RealTimeWIP',
})
</script>
  <style scoped>
.contioner {
  width: 100%;
  height: 100%;
  background: #fff;
  padding: 10px;
}

:deep(.ag-cell) {
  border-right: 1px solid #dde2eb;
}

:deep(.ag-header-cell) {
  border-right: 1px solid #dde2eb;
}

:deep(.ag-cell-value) {
  user-select: initial;
  -moz-user-select: text;
  -ms-user-select: text;
  -webkit-user-select: text;
}
</style>
      